package com.ym.controller.management;

import com.github.pagehelper.PageInfo;

import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;

import com.ym.medical.domain.entity.ConsumeDetailsEntity;
import com.ym.medical.domain.entity.FeedBackEntity;
import com.ym.medical.domain.entity.PatientDetailEntity;
import com.ym.medical.domain.entity.PatientHealthRecordsEntity;
import com.ym.medical.domain.param.*;
import com.ym.medical.domain.vo.PCMyDoctorVo;
import com.ym.medical.domain.vo.PCconsumeVo;
import com.ym.medical.domain.vo.PatientAccountVo;
import com.ym.medical.domain.vo.PatientVo;
import com.ym.medical.service.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 综合管理中心
 */
@Api(tags = "患者管理")
@Controller
@RequestMapping("/management/patient")
public class PatientDetailController extends BaseController {

	@Autowired
	private IPatientSV iPatientSV;

	@Autowired
	private IPatientHealthRecordsSV iPatientHealthRecordsSV;


	@Autowired
	private IFeedBackSV iFeedBackSV;

	@Autowired
	private IConsumeDetailsSV iConsumeDetailsSV;

	@Autowired
	private IUserCollectionSV iUserCollectionSV;


	@ApiOperation("患者列表查询检索")
	@GetMapping("/getPatientList")
	@ResponseBody
	public APIResponse<PageInfo<PatientVo>> getPatientList(
			@ApiParam(name = "pageNo", value = "页码", required = true)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "10")
					int limit,
			@ApiParam(name = "patientDetailModel", value = "患者信息", required = false)
					PatientDetailParam patientDetailModel) throws Exception {

		PageInfo<PatientVo> patientDetailPageInfo =
				iPatientSV.getPatientList(pageNo, limit, patientDetailModel);
		return successData(patientDetailPageInfo);
	}


	@ApiOperation("患者账户列表")
	@GetMapping("/getAccountList")
	@ResponseBody
	public APIResponse<PageInfo<PatientAccountVo>> getAccountList(
			@ApiParam(name = "pageNo", value = "页码", required = true)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "10")
					int limit,
			@RequestParam(required = false) String phoneNo,
			@RequestParam(required = false) String realName) {
		PageInfo<PatientAccountVo> accountVos = iPatientSV.getAccountList(pageNo, limit, phoneNo, realName);
		return successData(accountVos);
	}


	@ApiOperation("患者健康档案")
	@GetMapping("/getPatientHealthRecords")
	@ResponseBody
	public APIResponse<PatientHealthRecordsEntity> getPatientHealthRecords(
			@ApiParam(name = "patientId", value = "患者id", required = true)
			@RequestParam Long patientId) throws Exception {
		PatientHealthRecordsEntity patientHealthRecords = iPatientHealthRecordsSV.selectByPatientId(patientId);
		return successData(patientHealthRecords);
	}

	@ApiOperation("患者健康档案列表")
	@GetMapping("/patientHealthList")
	@ResponseBody
	public APIResponse<PageInfo<PatientHealthRecordsEntity>> patientHealthList(
			@ApiParam(name = "pageNo", value = "页码", required = true)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "10")
					int limit,
			@ApiParam(name = "patientHealthModel", value = "健康信息", required = false)
					PatientHealthRecordsParam patientHealthModel) {
		PageInfo<PatientHealthRecordsEntity> healthRecords =
				iPatientHealthRecordsSV.patientHealthList(pageNo, limit, patientHealthModel);
		return successData(healthRecords);
	}


	@ApiOperation("患者账户管理")
	@GetMapping("/getPatientAccount")
	@ResponseBody
	public APIResponse<Map<String, Object>> getPatientAccount(
			@ApiParam(name = "pageNo", value = "页码", required = true)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "10")
					int limit,
			@ApiParam(name = "consumeModel", value = "账户明细对象 传 userId")
					ConsumeDetailsParam consumeModel) throws Exception {
		Map<String, Object> map = new HashMap<>();
		//账户信息
		PCconsumeVo accountInfor =
				iConsumeDetailsSV.getPatientAccount(consumeModel.getUserId());
		//交易明细
		PageInfo<ConsumeDetailsEntity> consumeDetailsList =
				iConsumeDetailsSV.getConsumeDetails(pageNo, limit, consumeModel);

		map.put("accountInfor", accountInfor);
		map.put("consumeDetailsList", consumeDetailsList);
		return successData(map);

	}

	@ApiOperation("家人管理")
	@GetMapping("/getFamily")
	@ResponseBody
	public APIResponse getFamily(
			@ApiParam(name = "pageNo", value = "页码", required = true)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "10")
					int limit,
			@ApiParam(name = "patientDetailModel", value = "患者信息 传userId", required = false)
					PatientDetailParam patientDetailModel) throws Exception {

		PageInfo<PatientDetailEntity> pageInfo = iPatientSV.getFamily(pageNo, limit, patientDetailModel);

		return successData(pageInfo);


	}


	@ApiOperation("我的医生")
	@GetMapping(value = "/patientDoctor", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public APIResponse getPatientDoctor(
			@ApiParam(name = "pageNo", value = "页码", required = false)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "10")
					int limit,
			@ApiParam(name = "userId", value = "用户id", required = true)
			@RequestParam Long userId) throws Exception {
		PageInfo<PCMyDoctorVo> pageInfo = iPatientSV.getPatientDoctor(pageNo, limit, userId);
		return successData(pageInfo);
	}


	@ApiOperation("关注收藏")
	@GetMapping(value = "/getfocusAcollection", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public APIResponse<Map<String, Object>> getFocusAcollection(
			@ApiParam(name = "pageNo", value = "页码", required = false)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "10")
					int limit,
			@ApiParam(name = "userId", value = "用户id", required = true)
			@RequestParam Long userId,
			@ApiParam(name = "listModel", value = "通讯录对象", required = true)
					AddressListParam listModel) throws Exception {
		listModel.setUserId(userId);
		Map<String, Object> map = new HashMap<>();
		int topicTotal = iPatientSV.selectTopicTotal(userId);
		int articleTotal = iPatientSV.selectArticleTotal(userId);
		PageInfo<PCMyDoctorVo> pageInfo = null;
		if (null == listModel.getType() || "".equals(listModel.getType())) {
			return failMsg("参数不完整");
		}
		if ("01".equals(listModel.getType())) {
			pageInfo = iPatientSV.getFocusCollection(pageNo, limit, listModel, userId);
		} else if ("02".equals(listModel.getType())) {
			pageInfo = iUserCollectionSV.pcCollectionTopic(listModel, pageNo, limit);
		} else {
			pageInfo = iUserCollectionSV.pcCollectArticle(listModel, pageNo, limit);
		}
		Long doctTotal = pageInfo.getTotal();
		map.put("doctTotal", doctTotal);
		map.put("topicTotal", topicTotal);
		map.put("articleTotal", articleTotal);
		map.put("pageInfo", pageInfo);
		return successData(map);
	}


	@ApiOperation("患者反馈")
	@GetMapping("/patient/feedbackList")
	@ResponseBody
	public APIResponse feedbackList(
			@ApiParam(name = "pageNo", value = "页码", required = false)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "10")
					int limit,
			@ApiParam(name = "feedBackModel", value = "反馈意见对象", required = true)
					FeedBackParam feedBackModel) {
		PageInfo<FeedBackEntity> backPageInfo = iFeedBackSV.feedbackList(pageNo, limit, feedBackModel);
		return successData(backPageInfo);
	}


	@ApiOperation("删除家人")
	@PostMapping(value = "/deletePatient")
	@ResponseBody
	public APIResponse<Long[]> deletePerson(
			@ApiParam(name = "ids", value = "患者id", required = true)
			@RequestBody Long[] ids) throws Exception {
		int rows = iPatientSV.deleteByIds(ids);
		if (rows == 0) {
			return failMsg("操作失败");
		}
		return successMsg("操作成功");

	}

	@ApiOperation("家人新增")
	@PostMapping(value = "/addPatient")
	@ResponseBody
	public APIResponse<PatientDetailEntity> addPatient(
			@ApiParam(name = "patientDetail", value = "患者对象", required = true)
			@RequestBody PatientDetailEntity patientDetail) {
		int rows = iPatientSV.addPatient(patientDetail);
		if (rows != 1) {
			return failMsg("操作失败");
		}
		return successMsg("新增成功");
	}


	@ApiOperation("家人修改")
	@PostMapping(value = "/upPatient")
	@ResponseBody
	public APIResponse<PatientDetailEntity> upPatient(
			@ApiParam(name = "patientDetail", value = "患者对象", required = true)
			@RequestBody PatientDetailEntity patientDetail) {
		int rows = iPatientSV.upPatient(patientDetail);
		if (rows != 1) {
			return failMsg("操作失败");
		} else {
			return successMsg("操作成功");
		}

	}


}
